package com.lyj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.lyj.dao.OptionDao;
import com.lyj.pojo.Option;
import com.lyj.pojo.OptionQueryModel;
import com.lyj.util.base.BaseDaoImpl;
import com.lyj.util.base.BaseQueryModel;
import com.lyj.util.dao.DbHelper;

/**
 * 
 * @author 李艺江
 * @version 创建时间：2018年12月12日 上午11:09:37
 */

public class OptionDaoImpl extends BaseDaoImpl implements OptionDao {

	@Override
	public String getInsertSql(Object data) {

		Option option = (Option) data;
		return "insert into t_option(content,idx,subjectId) " + " values('" + option.getContent() + "',"
				+ option.getIdx() + "," + option.getSubjectId() + ")";

	}

	@Override
	public String getUpdateSql(Object data) {
		Option option = (Option) data;
		return "update t_option set content='" + option.getContent() + "',idx=" + option.getIdx() + ",subjectId="
				+ option.getSubjectId() + " where id=" + option.getId();
	}

	@Override
	public String getDeleteSql(int id) {
		return "delete from t_option where id="+id;
	}

	@Override
	public String getFindAllSql() {
		return null;
	}

	@Override
	public String getFindConditionSql(BaseQueryModel queryModel) {
		OptionQueryModel qm = (OptionQueryModel) queryModel;
		StringBuilder sb = new StringBuilder();
		sb.append("select * from t_option ");
		sb.append(" where 1=1 ");
		if (qm.getId() != null && qm.getId() > 0) {
			sb.append(" and id=" + qm.getId());
		}
		if (qm.getContent() != null && qm.getContent().trim().length() > 0) {
			sb.append(" and content='" + qm.getContent() + "'");
		}
		if (qm.getIdx() > 0) {
			sb.append(" and idx=" + qm.getIdx());
		}
		if (qm.getSubjectId() != null && qm.getSubjectId() > 0) {
			sb.append(" and subjectId=" + qm.getSubjectId());
		}
		return sb.toString();
	}

	@Override
	public Object rsToObject(ResultSet rs) throws Exception {
		Option option = new Option();
		option.setId(rs.getLong("id"));
		option.setContent(rs.getString("content"));
		option.setIdx(rs.getInt("idx"));
		option.setSubjectId(rs.getLong("subjectId"));
		return option;
	}

	@Override
	public int deleteOptions(Long subjectId) throws Exception {
		Connection con = DbHelper.getConnection();
		String sql = "delete from t_option where subjectId=" + subjectId;
		PreparedStatement pst = con.prepareStatement(sql);
		int rows = pst.executeUpdate();
		DbHelper.closeAll(null, pst, null);
		return rows;
	}

}
